package org.chatboleh.bean;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.chatboleh.exception.EDMException;
import org.chatboleh.util.MD5;

public class User extends DataObject {

	private String username;
	private String password;
	private String email;
	private boolean disabled;

	public User(long i) {
		super(i);
	}

	public User() {
	}

	@Override
	protected String fieldnames() {
		return "username,password,email,disabled";
	}

	@Override
	public void prepareUpdate() throws EDMException {
		try {
			ps.setString(1, this.username);
			ps.setString(2, this.password);
			ps.setString(3, this.email);
			ps.setBoolean(4, this.disabled);
		} catch (SQLException e) {
			throw new EDMException(e);
		}

	}

	@Override
	protected String tablename() {
		return "user";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		try {
//			logger.debug("Original Password = " + password);
			this.password = MD5.getInstance().hashData(password.getBytes());
//			logger.debug("Encripted Password = " + this.password);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e);
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public String toString() {
		StringBuffer buff = new StringBuffer("\nid\t=\t" + id + "\n");

		Field[] fields = this.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].getModifiers() == Modifier.PRIVATE) {
				try {
					buff.append(fields[i].getName()).append("\t=>\t").append(
							fields[i].get(this)).append("\n");

					// System.out.println(fields[i].getName() + " : values: " +
					// fields[i].get(this));
				} catch (IllegalAccessException ex) {
					ex.printStackTrace(System.out);
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace(System.out);
				}
			}
		}
		return buff.toString();

	}

	@Override
	protected void loadResultSetIntoObject(ResultSet resultset) {
		try {
			this.username = resultset.getString("username");
			this.password = resultset.getString("password");
			this.email = resultset.getString("email");
			this.disabled = resultset.getBoolean("disabled");
		} catch (SQLException e) {
			logger.error(e);
		}

	}

	@Override
	public List<User> getList(String criteria) throws EDMException {
		List<User> list = new ArrayList<User>();

		try {
			openConnection();
			prepareStatement(selectlistquery(criteria));
			executeQuery();
			while (resultset.next()) {
				User user = new User();
				user.id = resultset.getLong("id");
				user.loadResultSetIntoObject(resultset);
				list.add(user);
			}
		} catch (SQLException e) {
			throw new EDMException(e);
		} finally {
			closeStatement();
		}
		return list;
	}

}
